const path = require('path')
const { name } = require('./package.json')

// const BundleAnalyzerPlugin = require("webpack-bundle-analyzer").BundleAnalyzerPlugin;// 生产环境打包分析体积--执行yarn analyz会在dist文件生成report.html文件，打开可查看文件分析

function resolve(dir) {
    return path.join(__dirname, dir)
}

module.exports = {
    publicPath: process.env.NODE_ENV === 'production' ? `/${name}/` : '/',

    // 开启生产环境SourceMap，设为false打包时不生成.map文件
    productionSourceMap: false,

    // 设置为 true 或 'warning' 时，eslint-loader 会将 lint 错误输出为编译警告。默认情况下，警告仅仅会被输出到命令行，且不会使得编译失败。
    lintOnSave: 'error',

    chainWebpack: config => {
        config.resolve.alias
            .set('@', resolve('src'))
            .set('@lib', resolve('src/lib'))
            .set('@assets', resolve('src/assets'))
            .set('@mixins', resolve('src/mixins'))
            .set('@components', resolve('src/components'))
            .set('@config', resolve('src/config'))
            .set('@layouts', resolve('src/layouts'))
            .set('@views', resolve('src/views'))
            .set('@router', resolve('src/router'))
            .set('@service', resolve('src/service'))
            .set('@store', resolve('src/store'))
            .set('styles', resolve('src/styles'))
            .set('images', resolve('src/images'))
            .set('@utils', resolve('src/utils')) // alias config
        config.plugin('html').tap(args => {
            args[0].title = 'DEMO项目'
            args[0].buildHash = new Date().getTime()
            return args
        })
    },

    configureWebpack: config => {
        // 生产环境
        if (process.env.NODE_ENV === 'production') {
            // 生产环境去除console
            config.optimization.minimizer[0].options.terserOptions.compress.drop_console = true

            // return {
            //   plugins: [new BundleAnalyzerPlugin()]
            // };
        }
    },

    devServer: {
        // 设置让浏览器 overlay 同时显示警告和错误
        overlay: {
            warnings: true,
            errors: true,
        },
        proxy: {
            '/apis': {
                target: 'http://xxxx.xxx.xx',
                changeOrigin: true,
                pathRewrite: {
                    '^/apis': '/',
                },
            },
        },
    },
}
